package com.example.datn;

import com.example.datn.activities.AlbumPhotoActivity;
import com.example.datn.activities.LoadResourceActivity;
import com.example.datn.activities.PhotoActivity;
import com.example.datn.activities.TabTinTucActivity;
import com.example.datn.activities.ViewGridSingers;
import com.example.datn.activities.ViewInforActivity;
import com.example.datn.activities.ViewListVideoActivity;
import com.example.datn.activities.SongsViewActivity;
import com.example.datn.manager.ResourceManager;
import com.example.datn.manager.SingerManager;
import com.example.datn.manager.SongManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	
	private ImageButton imbPhoto, imbMusic, imbVideo, imbProfile, imbAbout, imbTinTuc;
	private AnimationDrawable frameAnimationQl;
	private ImageView QlMain;
	private TextView tv_singerName;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv_singerName = (TextView)findViewById(R.id.singerName_mainScreen);
		tv_singerName.setText(SingerManager.getInstance().getCurrentSinger().singerName);
		
		imbPhoto=(ImageButton)findViewById(R.id.imbPhoto);

		imbMusic = (ImageButton)findViewById(R.id.imbMusic);
		imbMusic.setOnClickListener(this);
		
		imbVideo = (ImageButton)findViewById(R.id.imbVideo);
		imbVideo.setOnClickListener(this);
		
		imbProfile = (ImageButton)findViewById(R.id.imbProfile);
		imbProfile.setOnClickListener(this);
		
		imbAbout = (ImageButton)findViewById(R.id.imbAbout);
		imbAbout.setOnClickListener(this);
		
		imbTinTuc=(ImageButton)findViewById(R.id.imbTintuc);
		imbTinTuc.setOnClickListener(this);
		
		//Khoi tao itent Activity Images.java khi click vao Image button Photo
		
		imbPhoto.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intenPhoto=new Intent(MainActivity.this,AlbumPhotoActivity.class);
				startActivity(intenPhoto);	
			}
		});
		
		//thiet lap chay anh dong Quang le
		QlMain=(ImageView)findViewById(R.id.imView);		
		QlMain.setBackgroundResource(R.drawable.my_animation);
		frameAnimationQl = (AnimationDrawable)QlMain.getBackground();
		QlMain.post(new Runnable() {			
			@Override
			public void run() {
				if(QlMain!=null){
					frameAnimationQl.start();
				}
			}
		});
					
		//--------------------------------------------------------
		boolean a = isNetworkConnected();
		if(a){
//			LoadListMedia loadMedia = new LoadListMedia();
//			loadMedia.loadMediaResource(this);
		}
		else
			Toast.makeText(this, R.string.error_connect_status,  
      			  Toast.LENGTH_LONG).show(); 
		
	}
	
	private boolean isNetworkConnected(){
		NetworkInfo networkInfo = null;
		ConnectivityManager connectionManager = 
				(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);	
		networkInfo = connectionManager.getActiveNetworkInfo();
		if (networkInfo != null && networkInfo.isConnectedOrConnecting()) 
		   return true;
		else
		   return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View view) {
		Intent i;
		switch (view.getId()) {
		case R.id.imbMusic:
			if(ResourceManager.getInstance().isNetworkConnected(getApplicationContext()) && 
        		SongManager.getInstance().getListSong().size() == 0)
				i=new Intent(getApplicationContext(), LoadResourceActivity.class);
			else
				i = new Intent(getApplicationContext(), SongsViewActivity.class);
			startActivity(i);
			break;

		case R.id.imbVideo:
			i = new Intent(getApplicationContext(), ViewListVideoActivity.class);
			startActivity(i);
			break;
			
		case R.id.imbProfile:
			i = new Intent(getApplicationContext(), ViewInforActivity.class);
			startActivity(i);
			break;
			
		case R.id.imbAbout:
//			i = new Intent(getApplicationContext(), ViewGridSingers.class);
//			startActivity(i);
			break;
			
		case R.id.imbTintuc:
			i = new Intent(getApplicationContext(), TabTinTucActivity.class);
			startActivity(i);
			break;
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		
		MediaPlayer media = SongManager.getInstance().getCurrentMediaPlayer();
		if(media != null){
			if(media.isPlaying())
				media.pause();
			SongManager.killMediaPlayer(media);
		}
	}

	
}
